package cn.shop.merchant.controller.user;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.model.user.MerchantRole;
import cn.shop.merchant.req.AssignRoleReq;
import cn.shop.merchant.req.MerchantRoleQueryReq;
import cn.shop.merchant.service.MerchantPermissionService;
import cn.shop.merchant.service.MerchantRoleService;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * 商户角色管理
 *
 * @author : 李一涵
 * @date : Created in 2021-11-17 13:44
 * @description :
 */
@RestController
@RequestMapping("/role")
public class MerchantRoleController {

	@Resource
	private MerchantRoleService roleService;

	@Resource
	private MerchantPermissionService permissionService;

	/**
	 * 通过角色id查询角色所有权限
	 *
	 * @param id 角色id
	 * @return 所有数据
	 */
	@GetMapping("/queryPermissionIdsByRoleId/{id}")
	public ApiResult<List<Long>> queryPermissionsByRoleId(@Validated @NotNull(message = "角色id不可为空") @PathVariable Long id) {
		return ApiResult.success(roleService.queryPermissionsByRoleId(id));
	}

	/**
	 * 获取所有角色
	 *
	 * @return 返回结果
	 */
	@GetMapping("/all")
	public ApiResult<List<Map<String, Object>>> allRole() {
		long uid = StpUtil.getSession().getLong(UID);
		List<Map<String, Object>> list = roleService.allRole(uid);
		return ApiResult.success(list);
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<MerchantRole> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(roleService.getById(id));
	}

	/**
	 * 分配角色
	 *
	 * @param req 实体对象
	 * @return 返回结果
	 */
	@PostMapping("/assignRole")
	@OperationLog(desc = "分配角色", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> assignRole(@RequestBody AssignRoleReq req) {
		long uid = StpUtil.getSession().getLong(UID);
		return ApiResult.result(roleService.assignRole(req, uid));
	}

	// /**
	//  * 分配权限
	//  *
	//  * @param req 实体对象
	//  * @return 返回结果
	//  */
	// @PostMapping("/assignPermission")
	// public ApiResult<Map<String, Object>> assignPermission(@RequestBody AssignPermissionReq req) {
	//     long uid = StpUtil.getSession().getLong(UID);
	//     return ApiResult.result(permissionService.assignPermissions(req, uid));
	// }

	/**
	 * 新增角色
	 *
	 * @param role 角色
	 * @return 返回结果
	 */
	@PostMapping("insert")
	@OperationLog(desc = "新增角色", type = OperationType.CREATE)
	public ApiResult<Map<String, Object>> insert(@RequestBody MerchantRole role) {
		boolean isSuccess = roleService.save(role);
		return ApiResult.result(isSuccess);
	}

	/**
	 * 更新角色
	 *
	 * @param role 角色
	 * @return 返回结果
	 */
	@PutMapping("update")
	@OperationLog(desc = "修改角色", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> update(@RequestBody MerchantRole role) {
		long uid = StpUtil.getSession().getLong(UID);
		boolean isSuccess = roleService.update(role, uid);
		return ApiResult.result(isSuccess);
	}

	/**
	 * 获取角色列表
	 *
	 * @param req 查询条件
	 * @return 返回结果
	 */
	@GetMapping("/list")
	public ApiResult<Map<String, Object>> list(MerchantRoleQueryReq req) {
		long uid = StpUtil.getSession().getLong(UID);
		Map<String, Object> data = roleService.list(req, uid);
		return ApiResult.success(data);
	}

	/**
	 * 删除角色
	 *
	 * @param ids 编号
	 * @return 返回结果
	 */
	@DeleteMapping("/delete/{ids}")
	@OperationLog(desc = "删除角色", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> delete(@PathVariable long[] ids) {
		long uid = StpUtil.getSession().getLong(UID);
		boolean isSuccess = roleService.delete(ids, uid);
		return ApiResult.result(isSuccess);
	}

	/**
	 * 获取角色信息
	 *
	 * @param id 编号
	 * @return 返回结果
	 */
	@GetMapping("/edit/{id}")
	public ApiResult<Map<String, Object>> info(@PathVariable long id) {
		long uid = StpUtil.getSession().getLong(UID);
		Map<String, Object> data = roleService.info(id, uid);
		return ApiResult.success(data);
	}

	/**
	 * 获取用户关联的角色
	 *
	 * @param id 用户id
	 * @author jy
	 * @since 2022/1/18 10:46
	 */
	@GetMapping("roleByUserId")
	ApiResult<List<Long>> getRoleByUserId(Long id) {
		return ApiResult.success(roleService.getRoleByUserId(id));
	}

}
